package src.D_WinSock;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

// 客户端
public class D02_TCP_Client {
    public static void main(String[] args) throws Exception {
        // 1、拿服务器ip和端口
        InetAddress serverIP = InetAddress.getByName("localhost");
        int port = 9999;
        // 2、创建一个socket连接至服务端
        Socket socket = new Socket(serverIP, port);
        // 3、使用字节流传东西
        OutputStream os = socket.getOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(os); // 创建序列化流，传多个文件数据
        Scanner scanner = new Scanner(System.in);
        D02_TCP_Bean bean = new D02_TCP_Bean(scanner.nextLine(),new File("src\\C_io\\jieMi.png")); // 传字符串和文件
        oos.writeObject(bean);
        // 4、通知服务器传输完毕
        socket.shutdownOutput();
        // 5、接收服务器的反馈
        InputStream is = socket.getInputStream();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        int b;
        while ((b = is.read()) != -1){
            bos.write(b);
        }
        System.out.println("S端反馈："+bos.toString());
        //6、释放资源
        if (bos.toString().equals("收到")){
            bos.close();
            is.close();
            scanner.close();
            oos.close();
            os.close();
            socket.close();
        }
    }

}
